package com.yunbao.hujin.mychoosealbum.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.yunbao.hujin.mychoosealbum.ChooseImgShow2Activity;
import com.yunbao.hujin.mychoosealbum.MainActivity;
import com.yunbao.hujin.mychoosealbum.R;
import com.yunbao.hujin.mychoosealbum.photoviewlib.PhotoView;
import com.yunbao.hujin.mychoosealbum.photoviewlib.PhotoViewAttacher;
import com.yunbao.hujin.mychoosealbum.utils.GlideUtil;

import java.util.ArrayList;

/**
 * 显示大图的适配器2 on 2017/12/4.
 */

public class AdapterShowChooseImg2 extends PagerAdapter {

    private Context mContext;
    private ArrayList<String> mData;    //图片路径
//    private Activity activity;

    public AdapterShowChooseImg2(Context mContext, ArrayList<String> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    /**
     * 初始化item
     */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        final PhotoView photoView = new PhotoView(mContext);
//        CommonDialogUtil.waitingDialog(mContext,"正在加载");
        GlideUtil.showImgCenterBitmap(mContext, mData.get(position))
                .listener(new RequestListener() {
                    @Override
                    public boolean onException(Exception e, Object model, Target target, boolean isFirstResource) {
                        photoView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Object resource, Object model, Target target, boolean isFromMemoryCache, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(photoView);

        container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
            @Override
            public void onPhotoTap(View view, float x, float y) {
                //图片点击事件
            }
        });
        return photoView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }


}
